package com.wsu.search.timer;

import java.util.Calendar;
import java.util.TimerTask;

/**
 * <p>Title: 定时器基本类</p>
*  <p> 具体的测试代码
* public class TestTimer implements TimerInterface  { <br>
  public TestTimer() {<br>
  }<br>
  public void run(){<br>
   System.out.println("test1="+Calendar.getInstance().getTime());<br>
   com.greenflower.basic.store.FileCopy fc = new  com.greenflower.basic.store.FileCopy();<br>
  try {<br>
    fc.copy("d:/test/tttttt.zip", "d:/test/t2.zip");<br>
  }<br>
  catch (IOException ex) {<br>
  }<br>
   System.out.println("test2=44444444444444444444");<br>
  }<br>
  public  void startTimer(){<br>
   MyTimer timer = new MyTimer();<br>
  Calendar cldr = Calendar.getInstance();<br>
  //cldr.set(cldr.get(Calendar.YEAR),cldr.get(Calendar.MONTH),cldr.get(Calendar.DAY_OF_MONTH),7,0);//每天晚上23：55运行
   timer.setUpdateCalendar(cldr);<br>
  timer.setSecond(1);<br>
  timer.setIsDelay(true);//是否通过延时的方式运行<br>
  timer.setIsAllowMulti(false);<br>
  timer.setTimerInterface(this);<br>
   timer.run();<br>
  }<br>
  public static void main(String[] args) {<br>
    TestTimer testTimer1 = new TestTimer();<br>
    testTimer1.startTimer();<br>
  }<br>
<br>
}<br>
</p>
 */


public class MyTimer {
  private int second = 1;
  private Calendar updateCalendar = null;
  //定时器启动开始时间
  private TimerInterface timerInterface = null; //定时程序接口
  private boolean isDelay = false; //是否通过延时方式执行,这样可以避免在定时某个时间不准时
  private boolean isAllowMulti = true; //是否允许并发，也就是同时开启n多个定时器，默认是允许的。
  public MyTimer(int second, TimerInterface timerInterface) {
    this.second = second;
    this.timerInterface = timerInterface;
    updateCalendar.set(updateCalendar.get(Calendar.YEAR),
                       updateCalendar.get(Calendar.MONTH),
                       updateCalendar.get(Calendar.DAY_OF_MONTH),
                       updateCalendar.get(Calendar.HOUR),
                       updateCalendar.get(Calendar.MINUTE),
                       updateCalendar.get(Calendar.SECOND));

  }

  /**
   * 设置是否是通过延时启动定时器
   * @param second int
   * @param timerInterface TimerInterface
   * @param isDelay boolean
   */
  public MyTimer(int second, TimerInterface timerInterface, boolean isDelay) {
    this.isDelay = isDelay;
    this.timerInterface = timerInterface;

  }

  public MyTimer() {

  }

  public void run() {
    java.util.Timer updateTimer = new java.util.Timer();
    java.util.TimerTask task = new java.util.TimerTask() {
      private boolean isRun = false; //并发同步
      public void run() {
        //write log
//        System.out.println("isAllowMulti =" + isAllowMulti);
//        System.out.println("isRun =" + isRun);
//        if (!isAllowMulti) { //不允许并发
//          if (!isRun) {
//            isRun = true;
        timerInterface.run();
//            isRun = false;
//          }
//          else {
//            System.out.println("上个线程尚未完成！");
//          }
//        }
//        else {
//          timerInterface.run();
//        }
      }
    };
    if (isDelay) { //如果是通过延时执行的方式,
      long delay = 0;
      if (this.updateCalendar != null) {
        Calendar currCld = Calendar.getInstance(); //当前的日历
        delay = this.updateCalendar.getTime().getTime() -
            currCld.getTime().getTime();
        if (delay < 0) {
          delay = second * 1000 + delay;
        }
        System.out.println(delay);
      }
      updateTimer.schedule(task, delay, second * 1000); //24*60*60*1000
    }
    else {
      updateTimer.schedule(task, updateCalendar.getTime(), second * 1000); //24*60*60*1000
    }

  }

  public static void main(String[] args) {
    //  MyTimer myTimer1 = new MyTimer();
  }

  public Calendar getUpdateCalendar() {
    return updateCalendar;
  }

  public void setUpdateCalendar(Calendar updateCalendar) {
    this.updateCalendar = updateCalendar;
  }

  public void setSecond(int second) {
    this.second = second;
  }

  public int getSecond() {
    return second;
  }

  public TimerInterface getTimerInterface() {
    return timerInterface;
  }

  public void setTimerInterface(TimerInterface timerInterface) {
    this.timerInterface = timerInterface;
  }

  public boolean isIsDelay() {
    return isDelay;
  }

  public void setIsDelay(boolean isDelay) {
    this.isDelay = isDelay;
  }

  public boolean isIsAllowMulti() {
    return isAllowMulti;
  }

  public void setIsAllowMulti(boolean isAllowMulti) {
    this.isAllowMulti = isAllowMulti;
  }

}

/**
 * 自定义任务器
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
class MyTask
    extends TimerTask {
  private boolean isRun = false; //并发同步
  private boolean isAllowMulti = true; //是否允许并发，也就是同时开启n多个定时器，默认是允许的。
  public MyTask() {

  }

  public void setIsAllowMulti(boolean isAllowMulti) {
    this.isAllowMulti = isAllowMulti;
  }

  public void run() {

  }
}
